package com.oracle.malldemo.dao.impl;

import com.oracle.malldemo.dao.ItemsDao;
import com.oracle.malldemo.domain.Items;
import com.oracle.malldemo.util.DruidDBUtil;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ItemsDaoImpl implements ItemsDao {

    @Override
    public List<Items> queryAllItemsListByCatalogName(String catalogName) {
        String sql = "";
        List<Object> argsList = new ArrayList<Object>();
        ResultSet rs = null;
        List<Items> itemsList = new ArrayList<Items>();
        if("all".equals(catalogName)){
            sql = "select * from items;";
        }else{
            sql = "select * from items where catalog=?";
            argsList.add(catalogName);
        }
        try{
            rs = DruidDBUtil.executeQuery(sql,argsList);
            while(rs.next()){
                Items i = new Items();
                i.setId(rs.getInt("id"));
                i.setName(rs.getString("name"));
                i.setPic(rs.getString("pic"));
                i.setPrice(rs.getDouble("price"));
                i.setCity(rs.getString("city"));
                i.setNumber(rs.getInt("number"));
                i.setCatalog(rs.getString("catalog"));
                i.setProperty(rs.getString("property"));
                i.setProvince(rs.getString("province"));
                i.setBuyCount(rs.getInt("buy_count"));
                i.setShopId(rs.getInt("shop_id"));
                i.setShopName(rs.getString("shop_name"));
                i.setDiscount(rs.getInt("discount"));
                i.setStatus(rs.getInt("status"));
                i.setCreateTime(rs.getString("create_time"));
                i.setModifyTime(rs.getString("modify_time"));
                i.setFlag(rs.getBoolean("flag"));
                i.setFreePost(rs.getBoolean("is_free_post"));
                i.setVersion(rs.getInt("version"));
                itemsList.add(i);
            }
            return itemsList;
        }catch(Exception ex){
            ex.printStackTrace();
            return itemsList;
        }finally{
            DruidDBUtil.closeAll();
        }
    }

    @Override
    public Items queryItemById(Integer id) {

        Items i = null;
        String sql = "select * from items where id = ?;";
        ResultSet rs = null;
        List<Object> argsList = new ArrayList<Object>();
        argsList.add(id);
        try{
            rs = DruidDBUtil.executeQuery(sql,argsList);
            if(rs.next()){
                i = new Items();
                i.setId(rs.getInt("id"));
                i.setName(rs.getString("name"));
                i.setPic(rs.getString("pic"));
                i.setPrice(rs.getDouble("price"));
                i.setCity(rs.getString("city"));
                i.setNumber(rs.getInt("number"));
                i.setCatalog(rs.getString("catalog"));
                i.setProperty(rs.getString("property"));
                i.setProvince(rs.getString("province"));
                i.setBuyCount(rs.getInt("buy_count"));
                i.setShopId(rs.getInt("shop_id"));
                i.setShopName(rs.getString("shop_name"));
                i.setDiscount(rs.getInt("discount"));
                i.setStatus(rs.getInt("status"));
                i.setCreateTime(rs.getString("create_time"));
                i.setModifyTime(rs.getString("modify_time"));
                i.setFlag(rs.getBoolean("flag"));
                i.setFreePost(rs.getBoolean("is_free_post"));
                i.setVersion(rs.getInt("version"));
            }
            return i;
        }catch(Exception ex){
            ex.printStackTrace();
            return i;
        }
        finally {
            DruidDBUtil.closeAll();
        }

    }
}
